{extend name="$base" /}
{block name="resources"}
<style>
.error{padding:5px 0 0;}
</style>
{/block}
{block name="main"}
<div class="space-10"></div>
<div class="set-style">
	<dl>
		<dt><span class="color-red">*</span>商品分类名称：</dt>
		<dd>
			<p><input id="category_name" type="text" placeholder="请输入商品分类名称" class="input-common"/></p>
			<p class="error">商品分类不能为空，且长度不能超过30个字</p>
		</dd>
	</dl>
	<dl>
		<dt><span class="color-red">*</span>商品分类简称：</dt>
		<dd>
			<p><input id="short_name" type="text" placeholder="请输入商品分类简称" class="input-common"/></p>
			<p class="hint">
				<span>商品分类简称一般在电脑端首页左部楼层导航显示使用</span>
			</p>
			<p class="error">商品分类简称不能为空，且长度不能超过20个字</p>
		</dd>
	</dl>
	<dl>
		<dt>上级分类：</dt>
		<dd>
			<select name="search_type" id="pid" class="select-common">
				<option value="0">顶级分类</option>
				{foreach name="category_list" item="v1"}
				<option value="{$v1['category_id']}">{$v1['category_name']}{if condition='$v1.applet_id neq 0'}({$v1.applet_name}){/if}</option>
				{foreach name="v1['child_list']" item="v2"}
				<option value="{$v2['category_id']}">&nbsp;&nbsp;&nbsp;&nbsp;{$v2['category_name']}</option>
				{/foreach} {/foreach}
			</select>
		</dd>
	</dl>
	<dl>
		<dt>商品类型：</dt>
		<dd>
			<select name="attr" id="attr_id" class="select-common">
				<option value="">选择类型</option>
				{volist name="goodsAttributeList" id="vo"}
				<option value="{$vo.attr_id}">{$vo.attr_name}</option>
				{/volist}
			</select>
		</dd>
	</dl>
	<dl>
		<dt>是否可见：</dt>
		<dd>
			<input id="is_visible" type="checkbox" class="checkbox" checked="checked" />
			<span class="help-inline" id="is_menuPrompt" style="display: none;"></span>
		</dd>
	</dl>
	<dl>
		<dt>排序：</dt>
		<dd>
			<p><input id="sort" type="number" placeholder="请输入序号" class="input-common harf" /></p>
		</dd>
	</dl>
	<dl>
		<dt>图片：</dt>
		<dd>
			<div class="upload-btn-common">
				<div>
					<input class="input-file" name="file_upload" id="uploadImg" type="file" onchange="imgUpload(this);" title="上传">
					<input type="hidden" id="logo" value="" />
				</div>
				<input type="text" id="text_logo" class="input-common" readonly="readonly" value="" />
				<em>上传</em>
				
				<img id="preview_logo" src="__STATIC__/blue/img/upload-common-select.png" data-html="true" data-container="body" data-placement="top" data-trigger="manual"/>

			</div>
			<p class="hint">
				<span>建议使用<i class="important-note">宽100</i>像素-<i class="important-note">高50</i>像素内的<i class="important-note">GIF</i>或<i class="important-note">PNG</i>透明图片；点击下方"保存"按钮后生效</span>
			</p>
		</dd>
	</dl>
	<dl>
		<dt>关键字：</dt>
		<dd>
			<p><input id="keywords"  placeholder="请输入关键字" class="input-common long"></p>
			<p class="error">关键字不能为空， </p>
			<p class="hint">用于网站搜索引擎的优化，关键字之间请用英文逗号分隔</p>
		</dd>
	</dl>
	<dl>
		<dt>描述：</dt>
		<dd>
			<p><textarea id="description" placeholder="请输入描述" class="textarea-common"></textarea></p>
		</dd>
	</dl>
	<dl>
		<dt>电脑端分类模板：</dt>
		<dd>
			{$template_url['pc_template_url']}&nbsp;<input type="text" class="input-common harf" name="" id="pc_custom_template" style="width: 80px;" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">&nbsp;.html
			<p class="hint">用户自定义模板必须存放在{$template_url['pc_template_url']}下，模板名只能由英文组成</p>
		</dd>
	</dl>
	<dl>
		<dt>手机端分类模板：</dt>
		<dd>
			{$template_url['wap_template_url']}&nbsp;<input type="text" class="input-common harf" name="" id="wap_custom_template" style="width: 80px;" onkeyup="value=value.replace(/[^\w\.\/]/ig,'')">&nbsp;.html
			<p class="hint">用户自定义模板必须存放在{$template_url['wap_template_url']}下，模板名只能由英文组成</p>
		</dd>
	</dl>
	<dl>
		<dt></dt>
		<dd>
			<button class="btn-common btn-big" onclick="addGoodsCategoryAjax();">保存</button>
			<button class="btn-common-cancle btn-big" onclick="javascript:history.back(-1);">返回</button>
		</dd>
	</dl>
</div>
<script src="__STATIC__/js/ajax_file_upload.js" type="text/javascript"></script>
<script src="__STATIC__/js/file_upload.js" type="text/javascript"></script>
<script>
//图片上传
function imgUpload(event) {
	var fileid = $(event).attr("id");
	var id = $(event).next().attr("id");
	var data = { 'file_path' : "goods_category" };
	uploadFile({
		url: __URL(ADMINMAIN + '/goods/uploadimage'),
		fileId: fileid,
		data : data,
		callBack: function (res) {
			if(res.code){
				$("#" + id).val(res.data.path);
				$("#text_" + id).val(res.data.path);
				$("#preview_"+ id).attr("data-src",__IMG(res.data.path));
				showTip(res.message,"success");
			}else{
				showTip(res.message,"error");
			}
		}
	});
}

//模块输入信息验证
function verify( category_name,short_name, keywords, sort, description){
	if(category_name == '' || category_name.length>30){
		$("#category_name").focus().parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
	if(short_name == '' || short_name.length>20){
		$("#short_name").focus().parent().next().show();
		return false;
	}else{
		$(".error").hide();
	}
/* 	if(keywords == ''){
		$("#keywords").focus().parent().next().show();
		return false;
	}else{
		$(".error").hide();
	} */
	return true;
}

//添加模块
var flag = false;
function addGoodsCategoryAjax() {
	var category_name = $("#category_name").val();
	var short_name = $("#short_name").val();
	var pid = $("#pid").val();
	var keywords = $("#keywords").val();
	var sort = $("#sort").val();
	var description = $("#description").val();
	var category_pic = $("#logo").val();
	if($("#is_visible").prop("checked")){
		var is_visible = 1;
	}else{
		var is_visible = 0;
	}
	var attr_id = $("#attr_id").val();
	if(attr_id.length>0){
		var attr_name = $("#attr_id").find("option:selected").text();
	}else{
		var attr_name = '';
	}
	var pc_custom_template = $("#pc_custom_template").val();
	var wap_custom_template = $("#wap_custom_template").val();

	if(verify(category_name,short_name, keywords, sort, description)){
		if(flag) return;
		flag = true;
		$.ajax({
			type : "post",
			url : "{:__URL('ADMIN_MAIN/goods/addgoodscategory')}",
			data : {
				'category_name' : category_name,
				'pid' : pid,
				'keywords' : keywords,
				'sort' : sort,
				'description' : description,
				'is_visible' : is_visible,
				"category_pic" : category_pic,
				"short_name" : short_name,
				"attr_id" : attr_id,
				"attr_name" : attr_name,
				"pc_custom_template" : pc_custom_template,
				"wap_custom_template" : wap_custom_template
			},
			success : function(data) {
				if (data["code"] > 0) {
					showTip(data['message'],"success");
					location.href = "{:__URL('ADMIN_MAIN/goods/goodscategorylist')}";
				}else{
					showTip(data['message'],"error");
					flag = false;
				}
			}
		});
	}
}
</script>
{/block}